﻿
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XS.Tunnel.Cad.SteelBracing.Infrastructure.Extensions;

namespace XS.Tunnel.Cad.SteelBracing.Infrastructure
{
    public interface ISysPoint
    {
        int No { get; set; }
        string Name { get; set; }
        string Code { get; set; }
        string BizTpyeCode { get; set; }
        Entity ToEntity();
        Curve ToCurve();
        bool IsArc();
        bool IsLine();

    }

    public abstract class BasePoint : ISysPoint
    {
        public int No { get; set; }
        public string Name { get; set; }
        public string Code { get; set; }
        public string BizTpyeCode { get; set; }
        public abstract bool IsArc();
        public abstract bool IsLine();
        public abstract Entity ToEntity();
        public abstract  Curve ToCurve();
    }

    public class ArcPoint : BasePoint
    {
        public ArcPoint()
        {

        }
        public ArcPoint(double oX, double oY, double r, double startAngle, double endAngle, double totalAngle)
        {
            OX = oX;
            OY = oY;
            R = r;
            StartAngle = startAngle;
            EndAngle = endAngle;
            TotalAngle = endAngle-startAngle;

            X0 = OX + R * Math.Cos(StartAngle.DegreeToAngle());
            Y0 = OY + R * Math.Sin(StartAngle.DegreeToAngle());

            X1 = OX + R * Math.Cos(EndAngle.DegreeToAngle());
            Y1 = OY + R * Math.Sin(EndAngle.DegreeToAngle());
            var middleDegree = MiddleDegreeCount(StartAngle, EndAngle);

            VerticalX = OX + R * Math.Cos(middleDegree.DegreeToAngle());
            VerticalY = OY + R * Math.Sin(middleDegree.DegreeToAngle());


        }


        /// <summary>
        /// 计算中间角度 
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public double MiddleDegreeCount(double start, double end)
        {
            if (start <= end)
            {
                return start + (end - start) / 2;
            }
            else
            {
                return start - (start - end) / 2;
            }
        }

        public override Entity ToEntity()
        {
            return new Arc(new Point3d(OX,OY,0),R, StartAngle.DegreeToAngle(), EndAngle.DegreeToAngle());
        }



        public double X0 { get; set; }
        public double Y0 { get; set; }
        public double X1 { get; set; }
        public double Y1 { get; set; }

        public double VerticalX { get; set; }
        public double VerticalY { get; set; }

        /// <summary>
        /// 圆心x
        /// </summary>
        public double OX { get; set; }
        /// <summary>
        /// 圆心y
        /// </summary>
        public double OY { get; set; }
        /// <summary>
        /// 半径
        /// </summary>
        public double R { get; set; }
        /// <summary>
        /// 起点角度
        /// </summary>
        public double StartAngle { get; set; }
        /// <summary>
        /// 端点角度
        /// </summary>
        public double EndAngle { get; set; }

        /// <summary>
        /// 总角度
        /// </summary>
        public double TotalAngle { get; set; }

        public override bool IsArc()
        {
            return true;
        }

        public override bool IsLine()
        {
            return false;
        }

        public override Curve ToCurve()
        {
            return ToEntity() as Curve;
        }
    }

    public class LinePoint : BasePoint
    {
        public LinePoint(double x, double y, double x1, double y1)
        {
            X = x;
            Y = y;
            X1 = x1;
            Y1 = y1;
        }

        public double X { get; set; }
        public double Y { get; set; }
        public double X1 { get; set; }
        public double Y1 { get; set; }

        public override bool IsArc()
        {
            return false;
        }

        public override bool IsLine()
        {
            return true;
        }

        public override Curve ToCurve()
        {
            return ToEntity() as Curve;
        }

        public override Entity ToEntity()
        {
           return new Line(new Point3d(X,Y,0),new Point3d(X1,Y1,0));  
        }
    }
}
